//
//  QRCodeCardViewController.swift
//  XNWeiBo_Swift
//
//  Created by xunan on 16/6/25.
//  Copyright © 2016年 xunan. All rights reserved.
//

import UIKit

class QRCodeCardViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 1.设置标题
        navigationItem.title = "我的名片"
        
        // 2.添加图片容器
        view.addSubview(iconView)

        // 3.布局
        iconView.xmg_AlignInner(type: XMG_AlignType.Center, referView: view, size: CGSize(width: 200, height: 200))
    
        // 4.生成二维码
        let qrCodeImage = createQRCodeImage()
        
        iconView.image = qrCodeImage
        
    }

    //MARK: - 懒加载
    private lazy var iconView: UIImageView = UIImageView()
    
    private func createQRCodeImage() -> UIImage? {
        // 1.创建滤镜
        let fliter = CIFilter(name: "CIQRCodeGenerator")
        // 2.还原默认属性
        fliter?.setDefaults()
        // 3.设置要生成的二维码数据
        fliter?.setValue("xunan623".dataUsingEncoding(NSUTF8StringEncoding), forKey: "inputMessage")
        // 4.从滤镜中取出图片
        let ciImage =  fliter?.outputImage
        // 5.创建一个头像
        let bgImage = createNonInterpolatedUIImageFormCIImage(ciImage!, size: 200)
        let icon = UIImage(named: "avatar_default")
        // 6.合并
        let newImage = createNewImage(bgImage, iconImage: icon!)
        // 返回生成的
        return newImage
    }
    
    private func createNewImage(BgImage: UIImage,iconImage: UIImage) -> UIImage {
        // 1.开启图片上下文
        UIGraphicsBeginImageContext(BgImage.size)
        // 2.绘制背景
        BgImage.drawInRect(CGRect(origin: CGPointZero, size: BgImage.size))
        // 3.绘制头像
        let width: CGFloat = 50
        let x: CGFloat = (BgImage.size.width - width) * 0.5
        let y: CGFloat = (BgImage.size.height - width) * 0.5
        iconImage.drawInRect(CGRect(x: x, y: y, width: width, height: width))
        // 4.取绘制的图片
        let newImage = UIGraphicsGetImageFromCurrentImageContext()
        // 5.关闭上下文
        UIGraphicsEndImageContext()
        // 6.返回
        return newImage!
    }
    
    /**
     根据CIImage生成指定大小的高清UIImage
     
     :param: image 指定CIImage
     :param: size    指定大小
     :returns: 生成好的图片
     */
    private func createNonInterpolatedUIImageFormCIImage(image: CIImage, size: CGFloat) -> UIImage {
        
        let extent: CGRect = CGRectIntegral(image.extent)
        let scale: CGFloat = min(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent))
        
        // 1.创建bitmap;
        let width = CGRectGetWidth(extent) * scale
        let height = CGRectGetHeight(extent) * scale
        let cs: CGColorSpaceRef = CGColorSpaceCreateDeviceGray()
        let bitmapRef = CGBitmapContextCreate(nil, Int(width), Int(height), 8, 0, cs, 0)!
        
        let context = CIContext(options: nil)
        let bitmapImage: CGImageRef = context.createCGImage(image, fromRect: extent)!
        
        CGContextSetInterpolationQuality(bitmapRef,  CGInterpolationQuality.None)
        CGContextScaleCTM(bitmapRef, scale, scale);
        CGContextDrawImage(bitmapRef, extent, bitmapImage);
        
        // 2.保存bitmap到图片
        let scaledImage: CGImageRef = CGBitmapContextCreateImage(bitmapRef)!
        
        return UIImage(CGImage: scaledImage)
    }
    

}
